package com.my.helper.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.my.helper.R;
import com.my.helper.activity.BeiWangActivity;
import com.my.helper.activity.CrateBieWangActivity;
import com.my.helper.adapter.BieWangAdapter;
import com.my.helper.bean.BeiWangData;
import com.my.helper.util.MyDataBaseHelper;
import com.my.helper.util.SharedPrefUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：Helper
 * 创建人：
 * 创建时间：2018/5/4 11:29
 */

public class FragmentBeiWang extends Fragment implements View.OnClickListener {
    private View view = null;
    private RefreshLayout refreshLayout;
    private RecyclerView mRecyclerView;
    private TextView tvCrateBeiWang;
    private MyDataBaseHelper mSql;
    private SharedPrefUtil sp;
    private List<BeiWangData> datas = new ArrayList<BeiWangData>();
    private GridLayoutManager mGridLayoutManager;
    private BieWangAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bei_wang, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sp = new SharedPrefUtil(getActivity(), "myHelper");
        initView();
        initListener();
    }

    private void initView() {
        refreshLayout = view.findViewById(R.id.refreshLayout_bw);
        mRecyclerView = view.findViewById(R.id.module_rv_daily_bw);
        tvCrateBeiWang = view.findViewById(R.id.tv_crate_bw);

        mSql = new MyDataBaseHelper(getContext(), "Helper.db", null, 1);

        mGridLayoutManager = new GridLayoutManager(getActivity(),
                1, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        adapter = new BieWangAdapter(getActivity());
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 清除数据
        datas.clear();
        int userId = Integer.valueOf(sp.getString("user_id", "0"));
        SQLiteDatabase db = mSql.getWritableDatabase();
        // 查询 MyMemorandum 表中符合 id 的数据
        Cursor cursor = db.query("MyMemorandum", null, "uid=?",
                new String[]{"" + userId}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String content = cursor.getString(cursor.getColumnIndex("content"));

                Log.i("mwb", "content:" + content);

                BeiWangData bean = new BeiWangData();
                bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                bean.setTime(cursor.getString(cursor.getColumnIndex("date")));
                datas.add(bean);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

        adapter.setDatas(datas);
        adapter.notifyDataSetChanged();
    }

    private void initListener() {
        tvCrateBeiWang.setOnClickListener(this);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) { // 下拉刷新
                refreshLayout.finishRefresh(1000, true);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1000, true, true);
            }
        });

        adapter.setListener(new BieWangAdapter.OnBWClickListener() {
            @Override
            public void onClickListener(int position) {
                goIntent(position);
            }
        });
    }

    private void goIntent(int position) {
        Intent intent = new Intent(getActivity(), BeiWangActivity.class);
        intent.putExtra("content", datas.get(position).getContent());
        intent.putExtra("time", datas.get(position).getTime());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_crate_bw: // 创建备忘
                Intent intent = new Intent(getActivity(), CrateBieWangActivity.class);
                startActivity(intent);
                break;
        }
    }
}
